#include "Md5Hasher.h"

//#include <iostream>

/*
	Md5Hasher
*/

/*	public	*/

Md5Hasher::Md5Hasher()
	:  _hashValidity(false) {
	_ensureBaseHashFunction();
	_hashFunction = _baseHashFunction->clone();
}

Md5Hasher::~Md5Hasher() {
	delete _hashFunction;
}

void Md5Hasher::process(unsigned char* data, int length) {
	_hashValidity = false;
	_hashFunction->update(data, length);
}

const Hash& Md5Hasher::getHash() const {
	Md5Hasher* self = const_cast<Md5Hasher*>(this);

	if (!_hashValidity) {
		self->_hashValidity = true;
		self->_hashFunction->final(self->_hash._data);

//		std::cout << "*** Md5Hasher::getHash() ";
//		for(int i = 0; i < HASH_SIZE; ++i) {
//			std::cout << (int)(_hash._data[i]) << " ";
//		}
//		std::cout << "\n";
	}

	return _hash;
}


/*	private	*/

Botan::HashFunction* Md5Hasher::_baseHashFunction = 0;

void Md5Hasher::_ensureBaseHashFunction() {
	if (_baseHashFunction == 0) {
		_baseHashFunction = Botan::get_hash("MD5");
	}
}

